package com.guli.mall.member.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.member.entity.MemberReceiveAddressEntity;
import com.guli.mall.member.service.MemberReceiveAddressService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 会员收货地址
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-10-18 18:44:16
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取memberReceiveAddress分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size, @RequestParam Map<String, Object> params) {
        PageUtils<MemberReceiveAddressEntity> page = memberReceiveAddressService.queryPage(currentPage, size, params);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取MemberReceiveAddress")
    public R info(@PathVariable("id") Long id) {
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }

    @PostMapping("/save")
    @ApiOperation("插入MemberReceiveAddress")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新MemberReceiveAddress")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除MemberReceiveAddress")
    public R delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getMemberReceiveAddressByMemberId/{memberId}")
    @ApiOperation("根据会员id获取会员的收获地址")
    public List<MemberReceiveAddressEntity> getMemberReceiveAddressByMemberId(@PathVariable("memberId") Long memberId) {

        return memberReceiveAddressService.getMemberReceiveAddressByMemberId(memberId);
    }

    @GetMapping("/calculateCarriage/{memberReceiveAddressId}")
    @ApiOperation("根据收货地址id来计算运费")
    public R calculateCarriage(@PathVariable("memberReceiveAddressId") Long memberReceiveAddressId) {

        MemberReceiveAddressEntity carriage = memberReceiveAddressService.calculateCarriage(memberReceiveAddressId);

        return R.ok().put("data", carriage);
    }
}
